<--- %%NOBANNER%% --> prop2_pr.sas
 BackForward
/*------------------<--- Start of Description -->--------------------\
| Power for two independent proportions;                             |
|--------------------<--- End of Description -->---------------------|
|--------------------------------------------------------------------|
|----------------<--- Start of Example and Usage -->-----------------|
| Arguements:                                                        |
|   - Required:                                                      |
|     n1,n2 = sample sizes for groups 1 and 2 respectively           |
|     y1 = true proportion under null hypothesis, 0------------|
|--------------------------------------------------------------------|
|----------------<--- Start of Example and Usage -->-----------------|
| Example: %prop2_pr(sides=1,y1=.05,min_y2=.15, n1=50,n2=50);        |
| Usage:   %prop2_pr (ALPHA=.05,SIDES=2,Y1=.,MIN_Y2=.,MAX_Y2=.,      |
|                    INC_Y2=.,N1=.,N2=., PLOT= );                    |
| Reference: Bergstralh, EJ.  SAS macros for sample size and power   |
|            calculations.  Proceedings of the 9th annual SAS Users  |
|            Group International Conference.                         |
|            Equation #'s 15 and 16.                                 |
\-------------------<--- End of Example and Usage -->---------------*/
%MACRO prop2_pr (ALPHA=.05,SIDES=2,Y1=.,MIN_Y2=.,MAX_Y2=.,
       INC_Y2=.,N1=.,N2=., PLOT= );
/*--------------------------------------------\
| Author:  Michael Riggs and Eric Bergstralh; |
| Purpose: Power for two independent          |
|          proportions;                       |
\--------------------------------------------*/
 OPTIONS MISSING=' ' NOCENTER;
   %LET PLOT=%UPCASE(&PLOT);
 DATA T1;
      ALPHA=&ALPHA;
      SIDES=&SIDES;
      Y1=&Y1;
      MIN_Y2=&MIN_Y2;
      MAX_Y2=&MAX_Y2;
      INC_Y2=&INC_Y2;
      N1=&N1;
      N2=&N2;
    ZALPHA=(PROBIT(1-ALPHA))*(SIDES=1) + (PROBIT(1-ALPHA/2))*(SIDES=2);
    IF MAX_Y2=. THEN DO;
      MAX_Y2=MIN_Y2+1; INC_Y2=MIN_Y2+2;  *NEED 1 EXEC OF DO;
    END;
    TY1=Y1;
    TN1=N1;
    TN2=N2;
    DO Y2=MIN_Y2 TO MAX_Y2 BY INC_Y2;
      PBAR=(TN1*TY1+TN2*Y2)/(TN1+TN2);
      *NORMAL APPROX. TO BINOMIAL;
       ZB_NML=(ABS(TY1-Y2)-ZALPHA*SQRT(PBAR*(1-PBAR)*(1/TN1+1/TN2)))/
               SQRT( TY1*(1-TY1)/TN1+Y2*(1-Y2)/TN2 );
       PR_NML=PROBNORM(ZB_NML);
      *NORMAL APPROX. FOLLOWING 2*ARCSIN(SQRT(PI)) TRANSFORMATION;
       AS_Y1=2*ARSIN(SQRT(TY1));
       AS_Y2=2*ARSIN(SQRT(Y2));
       ZB_ARS= ABS(AS_Y1-AS_Y2)/SQRT(1/TN1+1/TN2)-ZALPHA;
       PR_ARS=PROBNORM(ZB_ARS);
      *---------------------------------------------------;
       REL_RISK=Y2/TY1;
       ODD_RATO=(Y2*(1-TY1))/(TY1*(1-Y2));
       FORMAT REL_RISK ODD_RATO 10.3;
      *---------------------------------------------------;
       OUTPUT;
     END;

 LABEL Y1='Group 1*Proportion'
       Y2='Group 2*Proportion'
       REL_RISK='Relative*Risk'
       ODD_RATO='Odds*Ratio'
       pr_nml='Power*Normal@'
       pr_ars='Power*Arcsin@@';

 PROC PRINT SPLIT='*';
      ID Y1; var Y2 REL_RISK ODD_RATO
  PR_NML PR_ARS;
 TITLE2 'POWER FOR TWO INDEPENDENT PROPORTIONS,';
 title3"Alpha=&alpha, Sides=&sides, N1=&n1, N2=&n2";
 title4"Group 1 true proportion=&y1";
FOOTNOTE1 '@, Normal approximation to binomial.';
FOOTNOTE2
     '@@, Arcsin transformation followed by normal approximation';

%IF &MAX_Y2 NE . %THEN %DO;
    %IF &PLOT=P  %THEN %DO;
      PROC PLOT NOLEGEND;
      PLOT PR_NML*Y2='N' PR_ARS*Y2='A' / OVERLAY vaxis=0 to 1 by .2;
       FOOTNOTE1 'N=Normal approximation to the binomial';
       footnote2 'A=Arcsin transformation followed by normal approximation';
       LABEL PR_NML='Power'
       Y2='True Group 2 proportion';
    %END;
    %ELSE %IF &PLOT=G %THEN %DO;
      SYMBOL1  F=SPECIAL V=K H=1 I=j       L=2;
      SYMBOL2  F=SPECIAL V=M H=1   I=j     L=1;
          PROC GPLOT ;
      PLOT PR_NML*Y2 PR_ARS*Y2/OVERLAY vaxis=0 to 1 by .2;
       LABEL PR_NML='Power'
            Y2='True Group 2 proportion';
      FOOTNOTE1 F=SPECIAL   ' ' M=(+6,-.7) H=2   'K'
           F=TRIPLEX  H=1    M=(-.5,+.3)
           '  Normal approx. to binomial'
           F=SPECIAL M=(+6 -.4) H=2.0 'M'
           F=TRIPLEX  H=1    M=(-.5,+.4)
           '  Normal approx. with arcsin';
      run;
      quit;
    %END;
    RUN;
%END;
%MEND prop2_pr;